<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>H+ 后台主题UI框架 - 基础表格</title>
    <meta name="keywords" content="">
    <meta name="description" content="">

    <link rel="shortcut icon" href="../favicon.ico"> <link href="../css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="../css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="../css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="../css/animate.css" rel="stylesheet">
    <link href="../css/style.css?v=4.1.0" rel="stylesheet">
    <link href="../css/shop/style.css" rel="stylesheet">
    <link rel="stylesheet" href="../css/global.css">
</head>

<body class="gray-bg">
<div>
    <div class="row" style="padding:30px;">

        <div class="col-sm-12 animated fadeInRight" style="background: #fff;padding:30px 10px;">
            <div class="row">
                <div class="col-sm-2 m-b-xs">
                    <!--<select class="input-sm form-control input-s-sm inline" style="font-size: 12px">-->
                    <!--<option value="0">排序关键字</option>-->
                    <!--<option value="1">注册时间</option>-->
                    <!--<option value="2">订单</option>-->
                    <!--<option value="3">会员等级</option>-->
                    <!--</select>-->
                    <button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#myModal">添加类别</button>
                </div>
                <div class="col-sm-2 m-b-xs">
                    <!--<select class="input-sm form-control input-s-sm inline" style="font-size: 12px">-->
                    <!--<option value="0">正序</option>-->
                    <!--<option value="1">倒序</option>-->
                    <!--</select>-->
                    <!--<button class="btn btn-primary btn-sm" id="toExcel"> 导出</button>-->
                </div>
                <div class="col-sm-3 m-b-xs">
                    <!--<div data-toggle="buttons" class="btn-group">-->
                    <!--<label class="btn btn-sm btn-white">-->
                    <!--<input type="radio" id="option1" name="options">天</label>-->
                    <!--<label class="btn btn-sm btn-white active">-->
                    <!--<input type="radio" id="option2" name="options">周</label>-->
                    <!--<label class="btn btn-sm btn-white">-->
                    <!--<input type="radio" id="option3" name="options">月</label>-->
                    <!--</div>-->
                </div>
                <div class="col-sm-2">
                    <!--<select class="input-sm form-control input-s-sm inline" style="font-size: 12px" id="">-->
                        <!--<option value="0">选择查询字段</option>-->
                        <!--<option value="1">商品类别</option>-->
                        <!--<option value="2">门店</option>-->
                    <!--</select>-->

                </div>
                <div class="col-sm-3" style="font-size: 12px">
                    <!--<div class="input-group">-->
                    <!--<input type="text" id='searchContent' placeholder="请输入查询内容" class="input-sm form-control place-holder" >-->
                    <!--<span class="input-group-btn">-->
                    <!--<button type="button" class="btn btn-sm btn-primary" id="searchBtn">搜索</button>-->
                    <!--</span>-->
                    <!--</div>-->
                    <!--<select class="input-sm form-control input-s-sm inline" style="font-size: 12px" id="searchKey">-->
                        <!--<option value="0">选择商品类别</option>-->
                        <!--<option value="1">商品类别</option>-->
                        <!--<option value="2">门店</option>-->
                    <!--</select>-->
                </div>
            </div>
            <!--<div class="group-set">-->
            <!--<div>批量处理：</div>-->
            <!--<input type="checkbox"  class="i-checks" name="input[]" id="selectAll">-->
            <!--&lt;!&ndash;<button>删除</button>&ndash;&gt;-->
            <!--<button id="btn">确认发货</button>-->
            <!--<button id="cancleBtn">取消发货</button>-->
            <!--<button id="">确认收货</button>-->
            <!--</div>-->
            <div class="table-responsive" style="position: relative;padding-bottom:50px;">
                <table class="table table-striped">
                    <thead>
                    <tr>
                        <!--<th></th>-->
                        <th>类别名称</th>
                        <th>类别id</th>
                        <th>类别图标</th>
                        <th>类别选中图标</th>
                        <th>权值</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody id="class-list">
                    <tr>
                        <td>

                        </td>
                        <td>
                            商品名称
                        </td>
                        <td>
                            商品价格
                        </td>
                        <td>
                        商品类别
                    </td>
                        <td>
                            商品类别
                        </td>
                        <td>
                            <button class="btn btn-primary btn-sm edit" data-id="" data-toggle="modal" data-target="#myModal"> 修改</button>
                        </td>
                    </tr>
                    </tbody>
                </table>
                <!--<div id="page" class="page_div"></div>-->
            </div>

        </div>
    </div>
</div>

<!-- Button trigger modal -->
<!--<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">-->
    <!--Launch demo modal-->
<!--</button>-->

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">商品类别</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal">
                    <input type="hidden" id="id" >
                <div class="form-group">
                    <label for="inputClassName" class="col-sm-3 control-label">类别名称</label>
                    <div class="col-sm-9">
                        <input type="email" class="form-control" id="inputClassName" placeholder="请输入商品类别名称">
                    </div>
                </div>
                <div class="form-group">
                    <label for="qz" class="col-sm-3 control-label">类别权值</label>
                    <div class="col-sm-9">
                        <input type="number" class="form-control" id="qz" placeholder="请设置类别的权值">
                    </div>
                </div>
                <div class="form-group">
                    <label  class="col-sm-3 control-label" >类别图标</label>
                    <div class="col-sm-9">
                        <!--<input type="number" class="form-control" id="icon" placeholder="请设置类别的权值">-->
                        <input type="file" accept="image/*" id="icon">
                        <input type="hidden" id="iconUrl">
                    </div>
                </div>
                <div class="form-group">
                    <label  class="col-sm-3 control-label">类别选中图标</label>
                    <div class="col-sm-9">
                        <!--<input type="number" class="form-control" id="iconSelect" placeholder="请设置类别的权值">-->
                        <input type="file" accept="image/*" id="selectedIcon">
                        <input type="hidden" id="iconUrlSel">
                    </div>
                </div>


            </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="submitBtn">提交</button>
            </div>
        </div>
    </div>
</div>

<!-- 全局js -->
<script src="../js/jquery.min.js?v=2.1.4"></script>
<script src="../js/bootstrap.min.js?v=3.3.6"></script>

<script src="../js/plugins/jeditable/jquery.jeditable.js"></script>

<!-- Data Tables -->
<script src="../js/plugins/dataTables/jquery.dataTables.js"></script>
<script src="../js/plugins/dataTables/dataTables.bootstrap.js"></script>


<!-- Peity -->
<script src="../js/plugins/peity/jquery.peity.min.js"></script>
<!-- iCheck -->
<script src="../js/plugins/iCheck/icheck.min.js"></script>

<!-- Peity -->
<script src="../js/demo/peity-demo.js"></script>

<!--paging-->
<script src="../js/my-js/paging.js"></script>
<!-- 自定义js -->
<script src="../js/content.js?v=1.0.0"></script>
<script src="../js/my-js/global.js"></script>
<script src="../js/my-js/new-order.js"></script>
<script>
    $(document).ready(function(){
        var p = 1;
        classList();
        function classList(){
            getAjax(global.apiUrl+'/admin_get_shop_class',function(res){

                console.log(res);
                var html = '';
                for(var i=0; i<res.length;i++){
                    html += `
                         <tr>
                        <td>
                            ${res[i].shop_classname}
                        </td>
                        <td>
                            ${res[i].id}
                        </td>
                        <td>
                            <img src="${res[i].ico}" alt="">
                        </td>
                        <td>
                            <img src="${res[i].icoo}" alt="">
                        </td>
                        <td>
                            ${res[i].qz}
                        </td>
                        <td>
                            <button class="btn btn-primary btn-sm edit" data-id="${res[i].id}" data-toggle="modal" data-target="#myModal">修改</button>
                        </td>
                    </tr>
                `
                }
                $('#class-list').html(html);
            },{});

        }


        /*
        * 添加商品
        */
        //选择icon
        var icon = document.getElementById('icon');
        var selectedIcon = document.getElementById('selectedIcon');
        icon.addEventListener('change',function(e){
            console.log(1);
            imgUpload(icon,setIcon)
        })
        selectedIcon.addEventListener('change',function(){
            console.log(2);
            imgUpload(selectedIcon,setIconSel)
        })

        function setIcon(res){
            $('#iconUrl').val(global.apiUrl + res)
        }
        function setIconSel(res){
            $('#iconUrlSel').val(global.apiUrl+res)
        }
        function imgUpload(selector,fn){
            if(selector.files.length==0){
                alert('请先选择图片再点击上传')
            }else{
                for(var i=0;i<selector.files.length;i++){
                    uploadImg(selector.files[i],fn);
                }
            }
        }
        function uploadImg(f,fn) {
            var xhr = new XMLHttpRequest();
            var formData = new FormData();
            // for(var i=0; f=fileInput.files[i]; i++){
            //     formData.append('files', f);
            // }
            formData.append('file',f);
            xhr.onreadystatechange = function (e) {
                if(xhr.readyState == 4){
                    if(xhr.status == 200){
                        console.log(xhr.responseText);
                        fn(xhr.responseText)
                    }else {
                        console.log(xhr.responseText);
                    }
                }
            }

            xhr.open('POST', global.apiUrl + '/admin_upload', true);
            xhr.send(formData);
        }
        //提交新的商品类型
        $('#submitBtn').on('click',function(){
            var id = $('#id').val();
            var className = $('#inputClassName').val();
            var qz = $('#qz').val();
            var iconUrl = $('#iconUrl').val();
            var iconUrlSel = $('#iconUrlSel').val();
            console.log(className,qz,iconUrl,iconUrlSel);
            var data = {
                id:id,
                shop_classname:className,
                qz:qz,
                ico:iconUrl,
                icoo:iconUrlSel
            }
            if(className == ''){
                alert('请填写类别名称！')
            }else if(qz == ''){
                alert('请填写类别权值！')
            }else if(iconUrl == ''){
                alert('请选择图片！')
            }else if(iconUrlSel == ''){
                alert('请选择选中图片！')
            }else{
                getAjax(global.apiUrl+'/admin_save_shop_class',function(res){
                    console.log(res);
                    if(res == 1){
                        alert('提交成功！');
                        location.reload();
                    }
                },data)
            }
        })
    })
    //修改分类信息
    $(document).ready(function(){
        $('#class-list').on('click','.edit',function(){
            var id = $(this).data('id');
            $('#id').val(id);
            getAjax(global.apiUrl+'/admin_get_shop_class',function(res){
                $('#inputClassName').val(res.shop_classname);
                $('#qz').val(res.qz);
                $('#iconUrl').val(res.ico);
                $('#iconUrlSel').val(res.icoo);
            },{id:id})
        })
    })
    $('#toExcel').on('click',function(){
        window.location.href = global.apiUrl+'/add_excel'
    })

</script>






</body>

</html>
